package com.pany.sharedprefrences21;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText et_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_content = findViewById(R.id.et_content);

    }

    // 获取数据,
    public void get(View view) {

        SharedPreferences sPname_content = getSharedPreferences("sPname_content", MODE_PRIVATE);
        String content = sPname_content.getString("content", "");

        et_content.setText(content);
        et_content.setSelection(content.length());

    }

    //保存数据
    public void save(View view) {
        String contetn = et_content.getText().toString();

        SharedPreferences sPname_content = getSharedPreferences("sPname_content", MODE_PRIVATE);
        SharedPreferences.Editor edit = sPname_content.edit();
        edit.putString("content", contetn);
        edit.apply();// 提交一下,异步速度快,不确定成功
        boolean commit = edit.commit();//判断是否存储成功没有

        Toast.makeText(MainActivity.this, "保存成功了------", Toast.LENGTH_SHORT).show();
        et_content.setText("");

    }
}